07. Exercise: Set MyCanvasView as the Content View
22 06 AAK SetContentView SC V2
Exercise
In this exercise you are going to Set MyCanvasView as the content view.
- Open
strings.xmland define a string to use for the view's content description.
<string name="canvasContentDescription">Mini Paint is a simple line drawing app.
Drag your fingers to draw. Rotate the phone to clear.</string>
Open
MainActivity.kt.In
onCreate(), deletesetContentView(R.layout.activity_main).Create an instance of
MyCanvasView.
val myCanvasView = MyCanvasView(this)
- Below that, request the full screen for the layout of
myCanvasView. Do this by setting theSYSTEM_UI_FLAG_FULLSCREENflag onmyCanvasView. In this way, the view completely fills the screen.
myCanvasView.systemUiVisibility = SYSTEM_UI_FLAG_FULLSCREEN
- Add a content description.
myCanvasView.contentDescription = getString(R.string.canvasContentDescription)
- Below that, set the content view to
myCanvasView.
setContentView(myCanvasView)
Run your app.
You will see a completely white screen, because the canvas has no size, and you have not drawn anything yet.
Note: You will need to know the size of the view for drawing, but you cannot get the size of the view in the onCreate() method, because the size has not been determined at this point.